import React, { useEffect } from 'react';
import { splitFileUrl, downloadTemplate, getFileNameNotType } from '@/utils';
import { useDescriptionsType } from '@/components/DescriptionsType';
import { Form, Tooltip, message } from 'antd';
import TransferView from '../mview/TransferView';
import { UploadFormCommon } from '@/components/UploadForm';
import MyIcon from '@/components/MyIcon';

const ThemeContext = React.createContext({});

const getUpdateConfig = ({ pageType, record, openMaterialModalStatus, handleSelectContract, contractForm }) => {
  /** 表单数据 */
  const [acceptForm] = Form.useForm();

  const obj = {
    pageType: pageType,
  };
  /** 切换表单 */
  useEffect(() => {
    // 回填表单
    acceptForm?.setFieldsValue({
      ...record,
    });
  }, []);

  const disabled: boolean = pageType === 'view';

  /** 表单配置 */
  const [config, methods] = useDescriptionsType({
    labelWidth: 190,
    basicForm: acceptForm,
    name: 'accept-addAndEdit',
    schemas: [
      {
        key: 'accept-basic',
        title: '退库信息',
        fieldChildren: [
          {
            field: `applyTitle`,
            label: `退库标题`,
            component: 'Input',
            disabled: disabled,
            placeholder: '请选择退库标题',
            required: true,
          },
          {
            field: `applyTitle`,
            label: `单据号`,
            component: 'Input',
            disabled: disabled,
            placeholder: '自动带出',
            required: true,
          },
          {
            field: `applyFile`,
            label: `退库申请单`,
            component: 'Render',
            required: true,
            placeholder: '请上传退库申请单',
            render: (text, record) => {
              const fileName = 'applyFile';
              return (
                <>
                  {!disabled ? (
                    <UploadFormCommon
                      form={acceptForm}
                      fileFormName={fileName}
                      spanCol={24}
                      inRowProportion={24}
                      name={'attachFile'}
                      required={false}
                      // accept={'.doc|.docx'}
                      url={'/assetInfo/uploadFile'}
                      downloadUrl={'/assetInfo/downloadFile'}
                      fileFormDelName={'delFile'}
                      delFunc={(delFilePath: string) => {
                        console.log('--- w文件url', delFilePath);
                      }}
                      maxFile={1}
                      defaultFiles={text}
                      hintTitle={'且只能上传 1 个！'}
                      fieldBusiPath={'assetFile'}
                      // isShowUploadBtn={true}
                    >
                      上传
                    </UploadFormCommon>
                  ) : (
                    <div
                      style={{
                        height: '100%',
                        display: 'flex',
                        alignItems: 'center',
                        marginLeft: '8px',
                      }}
                    >
                      {record?.[fileName]
                        ? splitFileUrl(record?.[fileName])?.map((e, i) => {
                            return (
                              <Tooltip title={e.attachName} key={new Date().getTime() * 25615265152 + e?.attachName}>
                                <a
                                  onClick={() => {
                                    message.success('下载中，请稍后...');
                                    downloadTemplate('/assetInfo/downloadFile', e?.attachName, e?.attachUrl, () => {});
                                  }}
                                >
                                  <MyIcon type="icon-file-text-fill" />
                                  &nbsp;
                                  {getFileNameNotType(e.attachName, 8)}
                                </a>
                              </Tooltip>
                            );
                          })
                        : '--'}
                    </div>
                  )}
                </>
              );
            },
          },
          {
            field: `createrName`,
            label: `申请人`,
            component: 'Input',
            disabled: true,
            required: true,
            placeholder: '自动带出',
          },
          {
            field: `createDepartmentName`,
            label: `承办部门/团队`,
            component: 'Input',
            disabled: true,
            required: true,
            placeholder: '自动带出',
          },
          {
            field: `createDepartmentName`,
            label: `退库类型`,
            component: 'Select',
            disabled: false,
            required: true,
            placeholder: '请选择',
            options: [
              { value: '1', name: '资产退库' },
              { value: '0', name: '材料退库' },
            ],
            // onSelect: (val) => setIsShowUpload(val),
            fieldNames: {
              label: 'name',
              value: 'value',
            },
          },
          {
            field: `applyInfo`,
            label: `退库说明`,
            component: 'InputTextArea',
            disabled: disabled,
            placeholder: '请填写退库说明',
            required: false,
            colSpan: 2,
            allowClear: true,
            showCount: true,
            maxLength: 500,
            autoSize: { minRows: 2, maxRows: 4 },
          },
        ],
      },
      {
        key: 'accept-procurement',
        title: '资产信息',
        fieldCustomDom: (
          <ThemeContext.Provider value={obj}>
            <TransferView />
          </ThemeContext.Provider>
        ),
        // fieldCustomDom: <UpdateMaterielView openMaterialModalStatus={openMaterialModalStatus} contractForm={contractForm}/>,
      },
    ],
  });

  return {
    config,
    methods,
  };
};

export { getUpdateConfig, ThemeContext };
